package com.ahdy.controller;

import com.ahdy.entity.GoodPeople;
import com.ahdy.service.GoodPeopleService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 好人管理控制器
 */
@RestController
@RequestMapping("/api/good-people")
public class GoodPeopleController {

    @Autowired
    private GoodPeopleService goodPeopleService;

    /**
     * 添加好人
     *
     * @param goodPeople 好人实体
     * @return 响应结果
     */
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> addGoodPeople(@Valid @RequestBody GoodPeople goodPeople) {
        Integer id = goodPeopleService.addGoodPeople(goodPeople);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "添加好人成功");
        response.put("data", id);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新好人信息
     *
     * @param id         ID
     * @param goodPeople 好人实体
     * @return 响应结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateGoodPeople(@PathVariable Integer id,
            @Valid @RequestBody GoodPeople goodPeople) {
        goodPeople.setId(id);
        int rows = goodPeopleService.updateGoodPeople(goodPeople);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "更新好人信息成功");
        } else {
            response.put("code", 404);
            response.put("message", "好人不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 删除好人
     *
     * @param id ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteGoodPeople(@PathVariable Integer id) {
        int rows = goodPeopleService.deleteGoodPeople(id);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "删除好人成功");
        } else {
            response.put("code", 404);
            response.put("message", "好人不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID查询好人
     *
     * @param id ID
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getGoodPeopleById(@PathVariable Integer id) {
        GoodPeople goodPeople = goodPeopleService.getGoodPeopleById(id);

        Map<String, Object> response = new HashMap<>();
        if (goodPeople != null) {
            response.put("code", 200);
            response.put("message", "获取好人信息成功");
            response.put("data", goodPeople);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "好人不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 查询所有好人
     *
     * @return 响应结果
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllGoodPeople() {
        List<GoodPeople> goodPeopleList = goodPeopleService.getAllGoodPeople();

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取好人列表成功");
        response.put("data", goodPeopleList);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据手机号查询好人
     *
     * @param phone 手机号
     * @return 响应结果
     */
    @GetMapping("/by-phone/{phone}")
    public ResponseEntity<Map<String, Object>> getGoodPeopleByPhone(@PathVariable String phone) {
        GoodPeople goodPeople = goodPeopleService.getGoodPeopleByPhone(phone);

        Map<String, Object> response = new HashMap<>();
        if (goodPeople != null) {
            response.put("code", 200);
            response.put("message", "获取好人信息成功");
            response.put("data", goodPeople);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "好人不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 用户登录
     *
     * @param loginParams 登录参数
     * @return 响应结果
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> loginParams) {
        String phone = loginParams.get("phone");
        String password = loginParams.get("password");

        GoodPeople goodPeople = goodPeopleService.login(phone, password);

        Map<String, Object> response = new HashMap<>();
        if (goodPeople != null) {
            response.put("code", 200);
            response.put("message", "登录成功");
            response.put("data", goodPeople);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 401);
            response.put("message", "手机号或密码错误");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
        }
    }

    /**
     * 根据老人ID查询关联好人
     *
     * @param elderlyId 老人ID
     * @return 响应结果
     */
    @GetMapping("/by-elderly/{elderlyId}")
    public ResponseEntity<Map<String, Object>> getGoodPeopleByElderlyId(@PathVariable Integer elderlyId) {
        List<GoodPeople> goodPeopleList = goodPeopleService.getGoodPeopleByElderlyId(elderlyId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取关联好人列表成功");
        response.put("data", goodPeopleList);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据服务项目查询好人
     *
     * @param thing 服务项目
     * @return 响应结果
     */
    @GetMapping("/by-thing")
    public ResponseEntity<Map<String, Object>> getGoodPeopleByThing(@RequestParam String thing) {
        List<GoodPeople> goodPeopleList = goodPeopleService.getGoodPeopleByThing(thing);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取指定服务项目好人列表成功");
        response.put("data", goodPeopleList);
        return ResponseEntity.ok(response);
    }
} 